package service

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
	"notabug.org/ropemann/imabon/src/common"
	"notabug.org/ropemann/imabon/src/entities/post"
	"notabug.org/ropemann/imabon/src/entities/thread"
)

type ServiceTestSuite struct {
	suite.Suite
	board    string
	threadId string
}

func (s *ServiceTestSuite) SetupSuite() {
	common.LoadConfig("../../.env")

	s.board = "ani"
	s.threadId = "629bc57195be2b80c621320a"
}

func (s *ServiceTestSuite) TestGetMostRecentThreads() {
	recent := GetMostRecentThreads()
	assert.NotEmpty(s.T(), recent)
}

func (s *ServiceTestSuite) TestGetAllThreadsByBoard() {
	threads := GetAllThreadsByBoard(s.board)
	assert.NotEmpty(s.T(), threads)
}

func (s *ServiceTestSuite) TestGetAllThreadsByBoardEmpty() {
	threads := GetAllThreadsByBoard("invalid")
	assert.Empty(s.T(), threads)
}

func (s *ServiceTestSuite) TestGetAllBoards() {
	boards := GetAllBoards()
	assert.NotEmpty(s.T(), boards)
}

func (s *ServiceTestSuite) TestPersistThreadEmptyTitle() {
	thread := thread.NewThread("", "", "", "")
	res, err := PersistThread(s.board, *thread)
	assert.Empty(s.T(), res)
	assert.NotNil(s.T(), err)
}

func (s *ServiceTestSuite) TestPersistThreadEmptyBody() {
	thread := thread.NewThread("title", "", "", "")
	res, err := PersistThread(s.board, *thread)
	assert.Empty(s.T(), res)
	assert.NotNil(s.T(), err)
}

func (s *ServiceTestSuite) TestPersistThreadEmptyImage() {
	thread := thread.NewThread("title", "", "body", "")
	res, err := PersistThread(s.board, *thread)
	assert.Empty(s.T(), res)
	assert.NotNil(s.T(), err)
}

func (s *ServiceTestSuite) TestPersistPostEmpty() {
	post := post.NewPost("", "", "")
	res, err := PersistPost(s.board, "invalid", *post)
	assert.Empty(s.T(), res)
	assert.NotNil(s.T(), err)
}

func (s *ServiceTestSuite) TestGetThreadById() {
	thread := GetThreadById(s.board, s.threadId)
	b, _ := thread.ID.MarshalJSON()
	threadId := strings.ReplaceAll(string(b), "\"", "")
	assert.Equal(s.T(), s.threadId, threadId)
}

func TestServiceTestSuite(t *testing.T) {
	suite.Run(t, new(ServiceTestSuite))
}
